<?php

/**
 * 角色管理
 * @author Kevin_ren  <330202207@qq.com>
 */

namespace Admin\Controller;

use Common\Controller\BaseManageController;

class RoleController extends BaseManageController
{
    /**
     * 角色授权
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function auth()
    {
        $id       = I('id');

        $role     =  D('role')->find($id);
        $menu     =  D('Menu')->getAllData();
        $roleMaps =  D('RoleMaps')->getMenuIdsByRoleId($id);


        $this->assign('id', $id);
        $this->assign('menus', $menu);
        $this->assign('menuIds', $roleMaps);
        $this->assign('role', $role);
        $this->display();
    }

    /**
     * 保存
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function authSave()
    {
        $id        =  I('id');
        $menu_ids  =  I('post.menu_id');

        $obj = D('RoleMaps');
        $obj->where(array('role_id' => $id))->delete();

        foreach ($menu_ids as $val) {

            if (!empty($val)) {
                $data = array(
                    'role_id' => $id,
                    'menu_id' => $val,
                );
                $obj->add($data);
            }
        }
        $this->simpleSuccess('授权成功！', U('index'));
    }
}
